package cn.ujn.medicalbackend.service.impl;

import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.Sale;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.SaleAddRequest;
import cn.ujn.medicalbackend.request.SaleDeleteRequest;
import cn.ujn.medicalbackend.request.SaleUpdateRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ujn.medicalbackend.mapper.SaleMapper;
import cn.ujn.medicalbackend.service.SaleService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author Drink
* @description 针对表【sale】的数据库操作Service实现
* @createDate 2024-10-25 20:44:25
*/
@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale>
    implements SaleService{

    @Override
    public Page<Sale> salePageTransfer(Page<Sale> tempPage) {
        return tempPage;
    }

    @Override
    public long addSale(SaleAddRequest saleAddRequest, HttpServletRequest request) {
        Sale sale = new Sale();
        BeanUtils.copyProperties(saleAddRequest, sale);
        System.out.println(sale.toString());
        Date now = new Date();
        sale.setCreatetime(now);
        sale.setUpdatetime(now);
        // 3.校验信息
        if (sale.getSaleName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "不能为空");
        }
        boolean result;
        result = save(sale);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存失败");
        }

        return sale.getSaleId();
    }

    @Override
    public boolean updateSale(SaleUpdateRequest saleUpdateRequest, HttpServletRequest request) {
        if (saleUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        Sale sale =getById(saleUpdateRequest.getSaleId());
        if(sale==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "该销售地不存在，更新失败");
        }
        BeanUtils.copyProperties(saleUpdateRequest, sale);
        sale.setUpdatetime(now);

        boolean result =updateById(sale);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }

    @Override
    public boolean deleteSale(SaleDeleteRequest saleDeleteRequest, HttpServletRequest request) {
        // 鉴权，只有管理员可创建
        Long saleId = saleDeleteRequest.getSaleId();

        if (saleId == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；医生id和账户名不能为空");
        }
        QueryWrapper<Sale> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sale_id", saleId);
        long count = this.count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该药品");
        }

        boolean result = removeById(saleId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;
    }
}




